import { addMiddleware, IStateTreeNode } from 'mobx-state-tree';

export function cancelInstanceFlows(m: IStateTreeNode) {
  return addMiddleware(m, (call, next, abort) => {
    console.log('call', call);

    if (call.type === 'action') {
      // 不用再允许执行新的 action
      // return; // 对于 type:action 必须调用 next 或 abort
      // mobx-state-tree.module.js:3862 Uncaught Error: [mobx-state-tree] 
      // Neither the next() nor the abort() callback within the middleware  
      // for the action: "doFlow" on the node: Model was invoked.
      abort(null);
    } else if (call.type === 'flow_resume') {
      // return abort(undefined);
      return;
    } else if (call.type === 'flow_throw') {
      return abort(null);
    } else {
      console.warn(`mst instance is destroyed, do not call it'action`);
      // next(call);
    }
  });
}